
local file = io.open(arg[1],"rb");

assert(file,"open file error:",arg[1]);

local ii = table.insert;
local format = string.format;

local txt = file:read('*a');
txt = txt:gsub('\r\n','\n');
txt = txt:gsub('\\\n',function(s)
    return "";
end);

txt = txt:gsub('{\n','');
txt = txt:gsub('\n}','');

local out = io.stdout;


local format = string.format;
local function gen_drscan_tdi(tdi_data)
    local tdi_d = string.reverse(tdi_data);
    local len = 0;
    local hex = '';
    local save_hex = '';
    local tcl_list = {};
    local status = '';
    local bytes = 0;
    local KB = 0;
    if(tdi_data=='0')then
        --没有啥数据
        return;
    end

    for w in string.gmatch(tdi_d,"(%x%x)")do
        len = len + 1;
        hex = hex .. w;

        --最保险的是更改成len > 3, 以32位写
        if(len>63)then
            local tcl_str = [[drscan AG10K.tap %d 0x%s -endstate ]]
            tcl_str = format(tcl_str,len*8,string.reverse(hex));
            --print(tcl_str);
            out:write(status);
            out:write('\n');

            bytes = bytes + len;
            if(bytes>=1024*4)then
                KB = KB + (bytes/1024);
                out:write(format('puts "%d kbytes"\n',KB));
                bytes = 0;
            end


            out:write(tcl_str);
            status = 'DRPAUSE';
            save_hex =  string.reverse(hex) .. save_hex;
            hex = '';
            len = 0;
        end
    end
    --还有剩余数据
    if(len>0)then
        out:write('DRPAUSE\n');
        local tcl_str = [[drscan AG10K.tap %d 0x%s -endstate RUN/IDLE]]
        tcl_str = format(tcl_str,8*len,string.reverse(hex));
        print(tcl_str);
    else
        out:write('RUN/IDLE\n');
    end

    out:flush();
    assert(save_hex==tdi_data:sub(#tdi_data-#save_hex+1,#tdi_data),"gen_drscan_tdi error!\n");
end


local cmd = {};

cmd['set'] = function() end
cmd['usb_connect'] = function() end
cmd['usb_close'] = function() end
cmd['if'] = function() end


cmd['runtest'] = function() end

cmd['sir'] = function(cmd_list)
    local irlen = tonumber(cmd_list[2]);
    assert(irlen==10,"irlen not is 10");
    assert(cmd_list[3]=='-tdi',"the current format is not supported!");
    assert(tonumber('0x'..cmd_list[4]),"the current format is not supported!");
    if(cmd_list[3]=='-tdi')then
        local ir = tonumber('0x'..cmd_list[4]);
        local tcl_str = [[irscan AG10K.tap 0x%s -endstate RUN/IDLE]];
        tcl_str = format(tcl_str,cmd_list[4]);
        print(tcl_str);
    end
end

cmd['sdr'] = function(cmd_list)
    --print("sdr");
    local drlen = tonumber(cmd_list[2]);
    assert(cmd_list[3]=='-tdi');
    --有关超长的 tdi数据.openocd实际上是可行的
    --但调试器可能会出现数据问题
    if(drlen>64)then
        print("#------------------")
        gen_drscan_tdi(cmd_list[4]);
        print("#==================")
    else
        local tcl_str = [[drscan AG10K.tap %d 0x%s -endstate RUN/IDLE]]
        tcl_str = format(tcl_str,drlen,cmd_list[4]);
        print(tcl_str);
    end

    if(cmd_list[5]==nil)then return; end
    assert(cmd_list[5]=='-tdo');
    assert(cmd_list[7]=='-mask',cmd_list[7]);
end


for w in string.gmatch(txt,'([^\n]+)')do
    local cmd_list = {};

    for cmd_w in string.gmatch(w,'([^%s]+)') do
        ii(cmd_list,cmd_w);
    end

    local cmd_str = cmd_list[1];
    --判断是否存在命令.如果不存在.则输出错误
    local func = cmd[cmd_str];
    --print(cmd_str);
    assert(func,"error cmd:"..cmd_str);
    func(cmd_list);
end

--结束的时候处理runtest
print("runtest 100\n");

